package com.fengpb.friday.conductor.client.controller;

import com.fengpb.friday.conductor.cache.LocalWorkflowDefCache;
import com.fengpb.friday.conductor.common.dto.HttpStatusEnum;
import com.fengpb.friday.conductor.common.dto.Response;
import com.fengpb.friday.conductor.client.service.MetadataService;
import com.fengpb.friday.conductor.common.metadata.workflow.WorkflowDef;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("metadata")
public class MetadataController {

    private final MetadataService metadataService;

    private final LocalWorkflowDefCache workflowDefCache;

    public MetadataController(MetadataService metadataService, LocalWorkflowDefCache workflowDefCache) {
        this.metadataService = metadataService;
        this.workflowDefCache = workflowDefCache;
    }

    @PostMapping("registerOrUpdateWorkflow")
    public Response<Object> registerOrUpdateWorkflow(@RequestBody WorkflowDef def) {
        metadataService.registerOrUpdateWorkflow(def);
        return new Response<>(HttpStatusEnum.SUCCESS);
    }

    @GetMapping("getWorkflowDefList")
    public Response<List<WorkflowDef>> getWorkflowDefList() {
        List<WorkflowDef> result = workflowDefCache.getAll();
        return new Response<>(HttpStatusEnum.SUCCESS, result);
    }
}
